package org.essilab.analyzer.Configuration;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * This class is used to list the folder that has already been stored in the DB.
 * It's used as a workaround for the problem resulting on the large number of insertion done into the DB.
 *
 * @author Mohamed-Amine Chouchene, Ahcène Idinarere, Vincent Nock, Alain
 * Janin-Manificat
 */
public class BlockedFolder {

    private FileWriter _writer = null;       //writer associated to the folder file, in order to add new folders to the list
    private String _filename = null;        //path of the file containing the folders already analysed
    private List<String> _folders = null;  //list of falders name

    /**
     * Constructor, set the path of the file containing the folders already analysed.
     * @param filename  String - path of the file containing the folders already analysed.
     * @see BlockedFolder#init() 
     */
    public BlockedFolder(String filename) {
        this._filename = filename;
        this._folders = new ArrayList<String>();
        this.init();
    }

    /**
     * Read the file at BlockedFolder#_filename.
     * Store each line (folders name) in BlockedFolder#_filename.
     */
    private void init() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this._filename), "UTF8"));
            String line = null;
            while (null != (line = reader.readLine())) {
                line = line.replace("\n", "");
                if (null != line && !line.trim().isEmpty()) {
                    this._folders.add(line);
                }
            }
            reader.close();
            this._writer = new FileWriter(this._filename, true);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    /**
     * Tell whether or not the given folder name is in the folder list.
     * @param folder    String - folder name to look for.
     * @return  boolean - True is the folder exists in the list, false otherwise.
     */
    public boolean isBlockedFolder(String folder) {
        return this._folders.contains(folder);
    }

    /**
     * Get the number of folders in the list.
     * @return  The number of elements in BlockedFolder#_folders.
     */
    public int size() {
        return this._folders.size();
    }

    /**
     * Add the given folder name to the folder list and to the file list.
     * @param folder    String - the folder name to add.
     */
    public void blockedNewFolder(String folder) {
        try {
            this._writer.write(folder + "\n");
            this._folders.add(folder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  Try to close BlockedFolder#_writer.
     */
    public void close() {
        try {
            this._writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
